<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增首页轮播图')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-picture-add">
            <!--<div class="form-group">    -->
                <!--<label class="col-sm-2 control-label">标题：</label>-->
                <!--<div class="col-sm-9">-->
                    <!--<input name="title" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->
            <div class="form-group">
                <label class="col-sm-2 control-label">上传图片：</label>
                <div class="col-sm-9">
                    <div class="layui-upload-list">
                        <input id="photo" name="photo" class="form-control" type="hidden">
                        <img class="layui-upload-img" id="pic" style="width: 220px;height: 180px;">
                    </div>
                    <button type="button" class="layui-btn" id="addBtn">上传图片</button>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label">内容详情：</label>
                <div class="col-sm-9">
                    <input id="content" name="content" class="form-control" type="hidden">
                    <div class="summernote"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js"/>
    <script type="text/javascript">
        var prefix = ctx + "module/picture"
        $("#form-picture-add").validate({
            focusCleanup: true
        });

        $('.summernote').summernote({
            placeholder: '请输入内容',
            height: 200,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (result) {
                    console.log(result.url);
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

        //上传图片
        layui.use(['upload'], function () {
            var $ = layui.jquery
                , upload = layui.upload;
            //普通图片上传
            upload.render({
                elem: '#addBtn'
                , url: ctx + 'common/uploadImage_QiNiu'
                , size: 10000
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $("#pic").attr("src", result);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#photo").val(res.data.src);
                }
            });
        });

        function submitHandler() {
            if ($.validate.form()) {

                var pic =$("#photo").val();
                if(pic == null || pic == undefined || pic == '' ){
                    $.modal.alertWarning("请上传封面图！");
                    return;
                }

                var sHTML = $('.summernote').summernote('code');
                $("#content").val(sHTML);

                $.operate.save(prefix + "/add", $('#form-picture-add').serialize());
            }
        }
    </script>
</body>
</html>